========

SETUP

========

FILEPATH:

filepath = "https://raw.githubusercontent.com/shabanm2/Utqiagvik/main/Analysis_Ready_Data/"

PICK DATE RANGES:

years = c("2022", "2023")
seasons = c("Spring", "Summer","Fall")
sites = c("TNHA", "BEO", "SSMH")

# put season values for the season that has the start of the data
date_start = "2022-06-01" # data starts in June 2022 (YEAR-MO-DY) where day is always 01
# put season values for the season that has the last of the data
date_end = "2023-11-01" # data ends after November of 2023 (will get data up UNTIL date_end not after)

PICK OUTPUT:

scree = F # scree plot
eigen = T # eigenvectors and eigenvalues

Dates (for selecting date ranges; no need to edit)

spring_months = c("March", "April", "May")
summer_months = c("June","July","August")
fall_months = c("September", "October", "November")
winter_months = c("December", "January", "February")
spring_dates = data.frame(months=spring_months, start=c("-03-01","-04-01","-05-01"), end=c("-04-01","-05-01","-06-01"))

summer_dates = data.frame(months=summer_months, start=c("-06-01","-07-01","-08-01"), end=c("-07-01","-08-01","-09-01"))

fall_dates = data.frame(months=fall_months, start=c("-09-01","-10-01","-11-01"), end=c("-10-01","-11-01","-12-01"))

winter_dates = data.frame(months=winter_months, start=c("-12-01","-01-01","-02-01"), end=c("-01-01","-02-01","-03-01"))
all_dates = data.frame(matrix(nrow = 0, ncol = 4))
for(yur in years){
  # spring
  if("Spring" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, spring_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(spring_dates[i,1], paste0(yur, spring_dates[i, 2]), paste0(yur, spring_dates[i, 3]),"Spring"))
      }
    }
  }
  if("Summer" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, summer_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(summer_dates[i,1], paste0(yur, summer_dates[i, 2]), paste0(yur, summer_dates[i, 3]),"Summer"))
      }
    }
  }
  if("Fall" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, fall_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(fall_dates[i,1], paste0(yur, fall_dates[i, 2]), paste0(yur, fall_dates[i, 3]),"Fall"))
      }
    }
  }
  if("Winter" %in% seasons){
    for(i in c(1:3)){
      curdate = as.Date(paste0(yur, winter_dates[i, 2]))
      if(curdate >= date_start && curdate < date_end){
        all_dates <- rbind(all_dates, c(winter_dates[i,1], paste0(yur, winter_dates[i, 2]), paste0(yur, winter_dates[i, 3]),"Winter"))
      }
    }
  }
  
}
colnames(all_dates) = c("months","start","end","szn")

Packages

library(dplyr)
library(lubridate)
library(tidyverse)

Load all data

daily <- read.csv(paste0(filepath, "daily_2022_2024.csv"))
daily <- daily %>% select(-X) %>% select(-X.1) # get rid of index columns
daily$Date <- as.POSIXct(daily$date, format="%Y-%m-%d") # format dates
daily$fullname[daily$site == "BEO"] <- "BEO-BASE"
daily <- daily %>% filter(fullname == "TNHA-SA" | fullname == "TNHA-SC" | fullname == "SSMH-SB" | fullname == "SSMH-SA" | fullname == "BEO-BASE") %>% select(-c(winddir, date)) %>% mutate(aspect = case_when(fullname == "TNHA-SC" | fullname == "SSMH-SB" ~ "North", fullname == "TNHA-SA" | fullname == "SSMH-SA" ~ "South", .default = "N/A")) %>% filter(grounddepth == 8) %>% filter(Date >= "2022-06-19") # create "aspect" column and filter for top depth of soil and start date of when we started collecting data
# note: the data before June 19, 2022 was estimated by our gap-filling script and should be disregarded due to extrapolation

=========================

DEFINE FUNCTIONS

=========================

Temporal Range: Season Vertical Spatial Range: 30-45 cm Horizontal Spatial Range: stations across site (TNHA, SSMH, BEO) –> Average Total Site –> North vs South (except for BEO)

Filter by Site and Join Tables

pick_site <- function(cursite){

  big_df <<- daily %>% filter(site == cursite)
  
  if(szn == "Winter"){
    big_df <<- big_df %>% select(-solar)
  }
  return(big_df)
}

Filter by Date Range

pick_dates <- function(datemin, datemax, big_df){
  pca_df <<- big_df %>% filter(Date >= datemin) %>% filter(Date < datemax)
  
  # get rid of NAs
  pca_df <<- na.omit(pca_df)
  pca_df <<- unique(pca_df)
  return(pca_df)
}

Calculate PCA

calc_pca <- function(pca_df){
  pca <<- prcomp(pca_df[,6:(ncol(pca_df)-2)], center=TRUE, scale.=TRUE)

  #take out variables
  sd <- pca$sdev
  loads <<- pca$rotation
  rownames(loads) <<- colnames(pca_df[6:(ncol(pca_df)-2)])
  scores <<- pca$x
  
  var <- sd^2
  varPercent <- var/sum(var) * 100
  
  return(list("pca"=pca, "loads"=loads))
}

Make Scree Plot

make_scree <- function(pca){
  sd <- pca$sdev
  
  var <- sd^2
  varPercent <- var/sum(var) * 100
  
  barplot(varPercent, xlab="PC", ylab="Percent Variance", names.arg=1:length(varPercent), 
          las=1, ylim=c(0, max(varPercent)), col="gray")
  abline(h=1/ncol(pca_df[5:ncol(pca_df)])*100, col="red")
}

Display Eigenvectors and Eigenvalues

make_eigen <- function(pca){
  eigenvectors <- pca$rotation
  print("Eigenvectors (Loadings):")
  print(eigenvectors)
  
  print("Loadings Cutoff:")
  sqrt(1/ncol(pca_df[5:ncol(pca_df)])) # cutoff for "important" loadings
  
  # Access the eigenvalues (variances of the principal components)
  eigenvalues <- (pca$sdev)^2
  print("Eigenvalues:")
  print(eigenvalues)
}

===============

PCA PLOTS

===============

cursite = "BEO"
make_pca <- function(pca_df, szn, yr, cursite){
  if (cursite == "BEO")
  {
    SOUTH <<- pca_df$site == cursite
    # n <- "BEO"
  } else {
    SOUTH <<- pca_df$site == cursite & pca_df$aspect == "South"
    NORTH <<- pca_df$site == cursite & pca_df$aspect == "North"
    s <- pca_df$fullname[SOUTH][1]
    n <- pca_df$fullname[NORTH][1]
  }

    scaling <- 2
  textNudge <- 1.1
  limNudge <- 1.3
  
  xlimit <- seq(floor(min(scores[,1])*limNudge),ceiling(max(scores[,1])*limNudge), 1)
  ylimit <- seq(floor(min(scores[,2])*limNudge),ceiling(max(scores[,2])*limNudge), 1)
  
  plot(scores[, 1], scores[, 2], xlab="PCA 1", ylab="PCA 2", type="n", asp=1, 
       las=1, xaxt='n', yaxt='n')
  
  axis(side = 1, at=xlimit)
  axis(side = 2, at=ylimit)

  if (cursite == "BEO")
  {
    nvstext <- " "
  } else {
    nvstext <- " North v. South "
  }
  
  mindate = format(as.Date(min(pca_df$Date)), format="%B %d %Y")
  maxdate = format(as.Date(max(pca_df$Date)), format="%B %d %Y")
  
  title(paste0(szn, " ", yr," Principal Component Analysis:\n", site, nvstext, "\n(", mindate," - ", maxdate, ")"), adj=0.5)
  

  points(scores[SOUTH, 1], scores[SOUTH, 2], pch=16, cex=1, col="mediumturquoise")
   
  if(cursite != "BEO"){
    points(scores[NORTH, 1], scores[NORTH, 2], pch=16, cex=1, col="salmon")
     legend(x = "topright",          # Position
       legend = c(paste0(s, " (south)"), paste0(n, " (north)")),  # Legend texts
       col = c("mediumturquoise","salmon"),
       pch = 19)  #colors
  
  } else{
    legend(x = "topright",          # Position
       legend = "BEO",  # Legend texts
       col = "mediumturquoise",
       pch = 19) 
    
  }
  
   
  
  arrows(0, 0, loads[, 1]* scaling, loads[, 2]* scaling, length=0.1, angle=20, col="darkred", lwd=1.4)
   
  text(loads[1, 1]*scaling*(textNudge+0.4), loads[1, 2]*scaling*textNudge, rownames(loads)[1],   col="darkred", cex=1) # ground label
  
  text(loads[2, 1]*scaling*textNudge, loads[2, 2]*scaling*textNudge, rownames(loads)[2],   col="darkred", cex=1) # vwc label
  
  if(nrow(loads) > 2){
    text(loads[3, 1]*scaling*(textNudge+0.2), loads[3, 2]*scaling*textNudge, rownames(loads)[3],   col="darkred", cex=1) # airtemp label
  
    if(nrow(loads)>3){
      text(loads[4, 1]*scaling*textNudge-0.2, loads[4, 2]*scaling*textNudge, rownames(loads)[4],   col="darkred", cex=1) # solar or wind label
      
      if(nrow(loads)>4){
  
  text(loads[5, 1]*scaling*textNudge, loads[5, 2]*scaling*textNudge, rownames(loads)[5],   col="darkred", cex=1) # solar or wind label
    }

    }
  
  
  }
  
  
 
  #text(-3, 1]*scaling*textNudge, 1, "TNHA-SA \n(south)", col="mediumturquoise")
  #text(1, 1, "TNHA-SC \n(north)", col="salmon")
}
for(i in c(1:nrow(all_dates))){
  
  month <- all_dates$months[i]
  startdate <- all_dates$start[i]
  enddate <- all_dates$end[i]
  szn <<- all_dates$szn[i]
  yr <<- substr(all_dates$start[i], 1, 4)
  
  for(site in sites){
    big_df <- pick_site(site)
    pca_df <- pick_dates(startdate, enddate, big_df)
    
    if(nrow(pca_df) > 4){
      p <- calc_pca(pca_df)
      pca <- p$pca
      loads <- p$loads
      if(scree == T){
        make_scree(pca)
      }
      if(eigen == T){
        make_eigen(pca)
      }
      make_pca(pca_df, szn, yr, site)
              
    }
  }
  
}
## [1] "Eigenvectors (Loadings):"
##                  PC1        PC2         PC3        PC4        PC5
## groundtemp 0.5187474 -0.4435608  0.06352847  0.2997888 -0.6635102
## vwc        0.4046919  0.5694811  0.06401746 -0.6257754 -0.3409144
## airtemp    0.4128426 -0.5723214  0.09346907 -0.5083177  0.4846502
## solar      0.4737433  0.3511527  0.52899586  0.4642843  0.3960588
## windspeed  0.4150348  0.1675861 -0.83862555  0.2111515  0.2275593
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0559718 1.4275617 0.8138706 0.4187730 0.2838228

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3         PC4         PC5
## groundtemp  0.6253691 -0.03642962  0.26834573  0.02681131  0.73133993
## vwc        -0.4149103  0.11681552  0.88724687  0.16170376  0.02912901
## airtemp     0.5863511  0.01557190  0.36115271 -0.37678473 -0.61931567
## solar      -0.1513777  0.74189147 -0.06343748 -0.61451378  0.21220349
## windspeed   0.2646510  0.65907631 -0.07954858  0.67345314 -0.18897405
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.2046058 1.2929138 0.7455709 0.6036530 0.1532565

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3         PC4         PC5
## groundtemp -0.5648624 -0.04762957 -0.1552548  0.22301158  0.77770412
## vwc        -0.1074701  0.86489898 -0.3933134  0.23717495 -0.17161746
## airtemp    -0.4983464 -0.39162636 -0.1785241  0.49790251 -0.56435972
## solar      -0.5290007  0.02432379 -0.2008628 -0.80121198 -0.19308044
## windspeed   0.3757522 -0.30938378 -0.8654385 -0.06454712  0.09970856
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.88739857 1.13709010 0.64306416 0.25299128 0.07945588

## [1] "Eigenvectors (Loadings):"
##                   PC1          PC2        PC3        PC4        PC5
## groundtemp  0.5005885  0.139278597  0.5411127 -0.3514919 -0.5600563
## vwc        -0.4850438 -0.001575782 -0.3453066 -0.7454221 -0.2997320
## airtemp     0.5019415  0.250012536 -0.1913861 -0.4942587  0.6361039
## solar       0.4873282 -0.094587831 -0.7354245  0.1923294 -0.4191952
## windspeed  -0.1571927  0.953491407 -0.1023846  0.1987890 -0.1270627
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.8827909 0.9931040 0.4672808 0.4444790 0.2123453

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2          PC3         PC4         PC5
## groundtemp -0.58667566 -0.01882620  0.025413378 -0.42273712 -0.69000343
## vwc        -0.09522072  0.69606188 -0.693877707  0.14832095 -0.05445635
## airtemp    -0.58857062  0.05146664 -0.001613455 -0.36225292  0.72090639
## solar      -0.54774186 -0.13360289  0.117891847  0.81701282 -0.02684562
## windspeed  -0.01755485 -0.70330995 -0.709920275 -0.02360286  0.02242885
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.5561869 1.2947489 0.7113976 0.2996092 0.1380574

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2          PC3         PC4         PC5
## groundtemp  0.5272228 -0.4119719  0.124275456 -0.01219343  0.73261325
## vwc        -0.3745580 -0.5921042  0.002360014  0.71162693 -0.05196592
## airtemp     0.4834165 -0.5141271  0.122757457 -0.22204817 -0.66151874
## solar       0.4281293  0.1551753 -0.814332396  0.35154271 -0.07685318
## windspeed  -0.4059017 -0.4373622 -0.553482968 -0.56617203  0.13062892
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6958487 1.1283549 0.6378974 0.4461798 0.0917192

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4         PC5
## groundtemp  0.5530877 -0.3336903 -0.18911751  0.1519020 -0.72381293
## vwc        -0.3974952  0.3975353 -0.74053134 -0.1664253 -0.32844981
## airtemp     0.5118750 -0.1575360 -0.58942460 -0.1381457  0.58877907
## solar       0.4075547  0.4858120  0.26103558 -0.7159510 -0.13099805
## windspeed  -0.3285952 -0.6854091 -0.01693995 -0.6461875 -0.06628937
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.62723504 1.07204492 0.71710339 0.50808664 0.07553001

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2          PC3         PC4         PC5
## groundtemp  0.5678348 -0.1783765  0.312317167 0.009269662  0.74034958
## vwc        -0.4791918 -0.3001445  0.183268956 0.776764304  0.20817824
## airtemp     0.5360326 -0.1709436  0.414555647 0.336039631 -0.63140187
## solar       0.3750804 -0.2069657 -0.834816099 0.345624078  0.01029523
## windspeed   0.1411475  0.8977965  0.009806847 0.405182245  0.09884333
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.65125935 1.08469995 0.75768409 0.43943558 0.06692103

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3         PC4         PC5
## groundtemp -0.5594029 -0.06236768 -0.15914643 -0.22763598 -0.77848118
## vwc        -0.4203442 -0.30132723  0.68217816  0.51560854  0.03596436
## airtemp    -0.5183585 -0.18401612  0.03693136 -0.61807853  0.56040834
## solar      -0.4539134  0.18550117 -0.62228123  0.54214262  0.27999890
## windspeed   0.1888020 -0.91489866 -0.34742987  0.07993753 -0.01472187
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.93483683 0.97860489 0.60996313 0.40867001 0.06792514

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4          PC5
## groundtemp -0.5617404 -0.18397305 -0.1968255 -0.3233802 -0.712240504
## vwc         0.3004320  0.68480190 -0.6261589 -0.1136529 -0.189195552
## airtemp    -0.5497470  0.02273978 -0.3868633 -0.3062375  0.673659117
## solar      -0.4538308  0.22107871 -0.1142437  0.8538471 -0.055274355
## windspeed   0.2932647 -0.66918411 -0.6375499  0.2442783  0.006829898
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.69774641 1.00238464 0.70734668 0.52938179 0.06314047

## [1] "Eigenvectors (Loadings):"
##                     PC1         PC2        PC3        PC4         PC5
## groundtemp -0.663189401  0.04027158 -0.1278523 -0.1789687 -0.71427027
## vwc        -0.011544240  0.67858568 -0.2042088  0.6997313 -0.08979474
## airtemp    -0.618988569  0.23215754 -0.2274060 -0.2141774  0.68218076
## solar      -0.420589590 -0.42644145  0.5381514  0.5796960  0.12489077
## windspeed   0.002023956 -0.54968195 -0.7750046  0.3105268  0.02804639
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0986485 1.8134387 0.7084750 0.2742479 0.1051899

## [1] "Eigenvectors (Loadings):"
##                    PC1        PC2        PC3         PC4          PC5
## groundtemp -0.49187506  0.3589706  0.3450043 -0.62410375  0.347369586
## vwc        -0.46368270 -0.4011069 -0.4744103  0.20504210  0.597498287
## airtemp    -0.49793862  0.3014573  0.3811772  0.70743718 -0.124165911
## solar      -0.54178794 -0.2039039 -0.3049445 -0.25509889 -0.711914623
## windspeed   0.03975218  0.7601316 -0.6462350  0.05393919  0.009516472
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.7187767 1.1905698 0.7354727 0.2172284 0.1379524

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3        PC4         PC5
## groundtemp -0.5757028  0.25135167  0.05851454  0.2640940  0.72953344
## vwc        -0.4132803  0.03502202  0.82224829 -0.2106012 -0.32791424
## airtemp    -0.5314942  0.20481505 -0.41498768  0.3848498 -0.59602064
## solar      -0.4496213 -0.37507497 -0.36856224 -0.7191559  0.06431303
## windspeed   0.1145442  0.86773576 -0.11149393 -0.4696881 -0.02960466
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.66856794 1.15997724 0.77436873 0.37132121 0.02576488

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2        PC3           PC4         PC5
## groundtemp -0.46398043  0.26714677  0.8222862  0.0002303013  0.19287318
## vwc         0.49941854  0.24085400  0.3548516 -0.3887960347 -0.64458396
## airtemp    -0.04792994  0.92999757 -0.3501853  0.0466011000  0.08947541
## solar      -0.52295117 -0.02158839 -0.1166083  0.4211782419 -0.73148302
## windspeed  -0.50943898 -0.07252875 -0.2483918 -0.8180921809 -0.06510143
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.57053953 1.11467889 0.22389815 0.08011846 0.01076497

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4         PC5
## groundtemp  0.6257931  0.2461892  0.28200883 -0.1843771  0.65897646
## vwc        -0.1311544  0.5913390 -0.66107867  0.3413763  0.28205292
## airtemp     0.6102563  0.2941681 -0.25719928 -0.2392019 -0.64628423
## solar      -0.1351657  0.5900987  0.64457340  0.3883413 -0.25928738
## windspeed   0.4477711 -0.3936467 -0.04265762  0.8008999 -0.03581775
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9818820 1.4605147 0.8663109 0.5840019 0.1072905

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3        PC4         PC5
## groundtemp -0.5315852 -0.3427027  0.02204563 -0.2538429 -0.73147097
## vwc        -0.5023974  0.1458198  0.78447293  0.2304738  0.24045280
## airtemp    -0.5176662 -0.3215083 -0.37051491 -0.3164480  0.62548661
## solar      -0.4271435  0.4307294 -0.49656848  0.6095449 -0.11787857
## windspeed   0.1207649 -0.7565686  0.01595833  0.6409067  0.04476392
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.83154392 1.44065358 0.38403261 0.32609385 0.01767604

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3         PC4         PC5
## groundtemp -0.3632799  0.42745242 -0.8260632 -0.02406672  0.04850239
## vwc         0.5268384  0.23788039 -0.1610297  0.35564683 -0.71654655
## airtemp    -0.1191591  0.85067134  0.4974614 -0.11880140  0.02403618
## solar      -0.5358145 -0.18191275  0.1133285 -0.43095901 -0.69371574
## windspeed  -0.5377480 -0.06295523  0.1771377  0.82042470 -0.04888043
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.300034903 1.137727211 0.522550659 0.036854874 0.002832354

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp  0.5186027  0.2073129 -0.22271303 -0.70191493  0.38182054
## vwc         0.5312852  0.1857996 -0.12213016  0.70824815  0.40826866
## airtemp     0.1782115 -0.9490905  0.04408882 -0.02400655  0.25484848
## solar       0.5986301 -0.1041553 -0.10061409  0.01497695 -0.78768407
## windspeed  -0.2422188 -0.1043013 -0.96094542  0.06999547 -0.04621516
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.65993449 1.01109101 0.89987459 0.38618502 0.04291489

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3        PC4         PC5
## groundtemp  0.5635074 -0.02576923 -0.04986423  0.3708856  0.73603856
## vwc        -0.4969384 -0.30834942  0.34094468  0.7356926  0.02204466
## airtemp     0.5585201  0.07309266 -0.09781410  0.4733103 -0.67016605
## solar       0.3268446 -0.18087547  0.88693548 -0.2641734 -0.06336071
## windspeed   0.1293815 -0.93069765 -0.29163015 -0.1654993 -0.06800106
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.8792237 1.0119011 0.8216424 0.1796022 0.1076306

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2         PC3          PC4         PC5
## groundtemp -0.44384467 -0.493559898  0.29568692  0.431224833 -0.53480363
## vwc         0.56382370 -0.041491827 -0.29082050  0.771243171  0.03144322
## airtemp     0.03348954 -0.804586764 -0.07663123 -0.120127995  0.57551326
## solar       0.52641892 -0.327493417 -0.19868985 -0.452538855 -0.60939488
## windspeed  -0.45482560 -0.008078078 -0.88467094  0.002639002 -0.10207240
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6296877 1.4571137 0.5806113 0.1887523 0.1438350

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2         PC3          PC4          PC5
## groundtemp -0.64041141  0.06121735 -0.08294995  0.301876521  0.698652657
## vwc        -0.06190763 -0.79745617  0.59373492 -0.008688864  0.087375388
## airtemp    -0.64785804 -0.03906993 -0.01155910  0.273287143 -0.709883109
## solar      -0.16449828  0.57737895  0.75393437 -0.266730856  0.003387123
## windspeed  -0.37317684 -0.15944651 -0.26841665 -0.873478023  0.017450083
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0914148 1.0284922 0.9558375 0.8043603 0.1198953

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2        PC3        PC4          PC5
## groundtemp -0.56013356 -0.003030481  0.1366068  0.2472801  0.778737662
## vwc        -0.53256109 -0.185226358  0.2048799 -0.7814428 -0.171584649
## airtemp    -0.55260519  0.022571581  0.1735786  0.5489850 -0.602166056
## solar      -0.30631787  0.453392151 -0.8291686 -0.1075472 -0.038961185
## windspeed  -0.05853155 -0.871554986 -0.4708638  0.1234860 -0.002104842
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.03822328 1.09293280 0.71105891 0.11408111 0.04370389

## [1] "Eigenvectors (Loadings):"
##                   PC1          PC2         PC3        PC4          PC5
## groundtemp 0.59730590 -0.002222981  0.03435141 -0.4322726 -0.674671108
## vwc        0.08974949  0.761135020 -0.57751613  0.2559856 -0.116468831
## airtemp    0.60810489  0.111308018 -0.03715198 -0.2973686  0.726643374
## solar      0.47531356 -0.016390585  0.40812880  0.7772168 -0.056332186
## windspeed  0.19863575 -0.638756928 -0.70522742  0.2347716 -0.008366521
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.43686045 1.17217854 0.79773023 0.52319263 0.07003815

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2          PC3         PC4         PC5
## groundtemp  0.5242652  0.20359263  0.082370580  0.61787745 -0.54326661
## vwc         0.5160100  0.08553606  0.271521198 -0.75500449 -0.28750949
## airtemp     0.5006994  0.41462183 -0.003458656  0.10115349  0.75309021
## solar       0.2576989 -0.77594080  0.495208243  0.17706247  0.23436049
## windspeed  -0.3765250  0.42099672  0.821126154  0.08131742  0.01140085
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.02728099 1.07739837 0.56101359 0.24018681 0.09412024

## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2        PC3         PC4        PC5
## groundtemp  0.49546733 -0.24167430  0.4188558 -0.59152719 -0.4132324
## vwc         0.06986936  0.78975205 -0.2728841 -0.53382203  0.1094453
## airtemp     0.62199813 -0.03553462  0.1479180  0.10910567  0.7603104
## solar      -0.46229988 -0.45911938 -0.1009098 -0.59353153  0.4615471
## windspeed  -0.38604086  0.32531819  0.8473674  0.03075549  0.1617501
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.9833278 1.1664056 0.7952641 0.6995336 0.3554689

## [1] "Eigenvectors (Loadings):"
##                   PC1          PC2        PC3        PC4         PC5
## groundtemp -0.5305852 -0.260676198  0.3212546  0.0587147 -0.73747907
## vwc        -0.1018741 -0.900907253 -0.3124268 -0.1440781  0.24416957
## airtemp    -0.5217536  0.003229993  0.5043373  0.3024365  0.61801195
## solar      -0.4439563  0.212835557 -0.7183998  0.4905331 -0.02971232
## windspeed   0.4886481 -0.274052248  0.1695011  0.8023114 -0.11697972
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 3.05302874 1.11923567 0.54956362 0.26562890 0.01254307

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2          PC3       PC4        PC5
## groundtemp  0.2272370  0.7948439  0.266742708 0.3317312 -0.3679528
## vwc         0.4427129 -0.5626464  0.004964568 0.4967322 -0.4905780
## airtemp     0.5701671  0.1088441 -0.226420350 0.3215691  0.7130145
## solar      -0.4225890  0.1456847 -0.776048212 0.4274196 -0.1235160
## windspeed  -0.4986951 -0.1363124  0.524696749 0.5975929  0.3166989
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.6348532 1.2067381 0.7439352 0.2956042 0.1188692

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4        PC5
## groundtemp  0.3425618 -0.6097783  0.18575508  0.19493280 -0.6620560
## vwc        -0.5882162 -0.1361013  0.30318370 -0.67649032 -0.2931187
## airtemp     0.4940593 -0.4376184  0.07663211 -0.53382530  0.5230235
## solar       0.4413930  0.4593978 -0.41615090 -0.46804672 -0.4493068
## windspeed   0.3126192  0.4550721  0.83337905  0.01802442 -0.0182519
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 1.8530097 1.6122229 0.6979682 0.5190553 0.3177440

## [1] "Eigenvectors (Loadings):"
##                    PC1        PC2         PC3       PC4          PC5
## groundtemp -0.64537581  0.1749576 -0.12827615 0.1535622  0.716131123
## vwc         0.40053851  0.4675300 -0.09756128 0.7794920  0.062118061
## airtemp    -0.59392749  0.3273412 -0.22910949 0.1347769 -0.685158305
## solar      -0.26192555 -0.5503956  0.56395763 0.5446467 -0.117351888
## windspeed   0.04128081 -0.5837106 -0.77684468 0.2323919 -0.009175683
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.12121115 1.49872356 0.75708097 0.53413652 0.08884781

## [1] "Eigenvectors (Loadings):"
##                    PC1          PC2        PC3        PC4         PC5
## groundtemp -0.62137497  0.069676881 -0.2831499  0.1218068  0.71695713
## vwc        -0.37253358 -0.483940092  0.3398694 -0.7149150 -0.02015200
## airtemp    -0.61624779 -0.007940019 -0.3204821  0.1936658 -0.69279176
## solar      -0.30759482  0.522841992  0.7742511  0.1756082 -0.04145697
## windspeed  -0.02712874 -0.698226559  0.3195957  0.6369576  0.06234800
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.37485403 1.52490796 0.57295458 0.48343221 0.04385123

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2        PC3        PC4         PC5
## groundtemp -0.4894196  0.24173580 -0.4400715 -0.6321460  0.32978895
## vwc        -0.1195654 -0.96763100 -0.1582017 -0.1539919  0.02555569
## airtemp    -0.5305440  0.05523545 -0.3524142  0.3101331 -0.70362904
## solar       0.4116833  0.02569158 -0.7913480  0.3738383  0.25272526
## windspeed  -0.5433346 -0.03928160  0.1757325  0.5837297  0.57586654
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.9074020 0.9980963 0.7473896 0.2158059 0.1313061

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3        PC4          PC5
## groundtemp -0.5715182  0.2914407 -0.0801825 -0.2487291  0.721203024
## vwc         0.3125595  0.8022385 -0.1740434  0.4731746  0.067339704
## airtemp    -0.5467687  0.3758942 -0.1707878 -0.2411337 -0.687338090
## solar      -0.4858559 -0.3194014 -0.2163909  0.7842262 -0.009539335
## windspeed  -0.2016469  0.1678156  0.9419587  0.2026904 -0.052980310
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.60645553 0.97668403 0.95828707 0.39058507 0.06798829

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2         PC3         PC4         PC5
## groundtemp -0.2733364 -0.6965913  0.59306217 -0.13483174  0.26484986
## vwc        -0.5702604  0.1549024  0.22957221 -0.13147043 -0.76211577
## airtemp    -0.4984584  0.2024279  0.05541174  0.78772563  0.29492374
## solar      -0.4853900  0.3868567 -0.12792572 -0.58642833  0.50445550
## windspeed   0.3406260  0.5478418  0.75903782 -0.01122862  0.08705596
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.4304098 1.1751153 0.6308195 0.5241039 0.2395515

## [1] "Eigenvectors (Loadings):"
##                   PC1        PC2        PC3         PC4        PC5
## groundtemp -0.4775224  0.4820039 0.08879957 -0.69454464 -0.2221869
## vwc         0.1587978 -0.7681367 0.17802634 -0.58333663 -0.1130236
## airtemp    -0.5707893 -0.3060000 0.07881668  0.39672358 -0.6457259
## solar      -0.5610982 -0.1884892 0.40952549  0.08699419  0.6887390
## windspeed   0.3257667  0.2201685 0.88684726  0.11121394 -0.2157203
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0839193 1.3162225 0.8845549 0.4081203 0.3071830

## [1] "Eigenvectors (Loadings):"
##                   PC1         PC2         PC3         PC4        PC5
## groundtemp -0.5678395 -0.19579481 -0.05843816  0.63394503  0.4836542
## vwc         0.3673016 -0.70364986 -0.21412522  0.40057637 -0.4045434
## airtemp    -0.4125278 -0.59048268 -0.20077230 -0.65469828  0.1105065
## solar      -0.5946342  0.08512644  0.27401486  0.08797648 -0.7458818
## windspeed   0.1374000 -0.33260330  0.91397030 -0.03581304  0.1840435
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.1758477 1.0940261 0.9917882 0.4785270 0.2598109

If you want to just make one plot (for testing):

  # look at all_dates and pick a row to use (set to i)

i <- 8
site <- "BEO"

month <- all_dates$months[i]
startdate <- all_dates$start[i]
enddate <- all_dates$end[i]
szn <<- all_dates$szn[i]
yr <<- substr(all_dates$start[i], 1, 4)

big_df <- pick_site(site)
pca_df <- pick_dates(startdate, enddate, big_df)


if(nrow(pca_df > 0)){
    p <- calc_pca(pca_df)
    pca <- p$pca
    loads <- p$loads # not used at the moment
    if(scree == T){
      make_scree(pca)
    }
    if(eigen == T){
      make_eigen(pca)
    }
    make_pca(pca_df, szn, yr, site)
            
  }
## [1] "Eigenvectors (Loadings):"
##                    PC1         PC2         PC3          PC4          PC5
## groundtemp -0.64041141  0.06121735 -0.08294995  0.301876521  0.698652657
## vwc        -0.06190763 -0.79745617  0.59373492 -0.008688864  0.087375388
## airtemp    -0.64785804 -0.03906993 -0.01155910  0.273287143 -0.709883109
## solar      -0.16449828  0.57737895  0.75393437 -0.266730856  0.003387123
## windspeed  -0.37317684 -0.15944651 -0.26841665 -0.873478023  0.017450083
## [1] "Loadings Cutoff:"
## [1] "Eigenvalues:"
## [1] 2.0914148 1.0284922 0.9558375 0.8043603 0.1198953